//============= 虚拟供应商销售订单-invented_vendor_order.js


//订单序号
var vendorOrderStyle = ["ModelTableColumn2", "ModelTableColumn104", "ModelTableColumn27", "ModelTableColumn51", 'ModelTableColumn98', "ModelTableColumn75", "ModelTableColumn103", "ModelTableColumn100", "ModelTableColumn104"];
// 商品来源
var GoodsSrcStyle = ["ModelTableColumn25", "ModelTableColumn28", "ModelTableColumn52", "ModelTableColumn76"];
//订单商品状态
var VendorGoodsStatusStyle = ["ModelTableColumn4", "ModelTableColumn29", "ModelTableColumn53", "ModelTableColumn77"];


// 封装了axiosService 方便调用其他应用的服务
const axiosService = function (url, param) {
    return new Promise(function (resolve, reject) {
        h3ui.axios({
            method: 'post',
            url,
            data: param,
            headers: {
                Authorization: 'Bearer ' + localStorage.getItem("token"),
                'Content-Type': 'application/json;charset=UTF-8'
            }
        }).then(function (res) {
            console.log(res);

            if (res.status == 200) {
                resolve(res.data);
            } else {
                reject(res);
            }
        }).catch(function (error) {
            h3ui.$message.error(error);
        });
    })
}


//获取系统加载列表服务
function customConditionLoad(evt, custCondArr, custListBo) {
    let {pageIndex, pageSize, filter} = evt.data;

    let listId = evt.node.id;
    let schemaCode = h3ui.widgets[listId].props.bindingModel.schemaCode;
    let dataSource = h3ui.models[schemaCode];
    filter = JSON.parse(JSON.stringify(filter)) || {};

    let condition = dataSource.toQueryCondition(filter, dataSource.schema);

    if (custCondArr) {
        if (!condition) {
            condition = {conditions: [], nature: "AND"};
        }

        condition.conditions = condition.conditions.concat(custCondArr);
    }

    let param = {
        condition: condition,
        currentLevel: false,
        displayColumn: {},
        page: pageIndex,
        pageIndex: pageIndex - 1,
        pageSize: pageSize,
        schemaCode: schemaCode,
        size: pageSize,
        tree: false,
    }

    let doService;
    // 是调用系统的listBo
    if (custListBo) {
        h3ui.services[custListBo](param).then(res => {
            if (res.success) {
                h3ui.models[schemaCode].loadItemsRefProp(res.data.data);

                h3ui.$message.loading("数据加载中...", 0.5, function () {
                    h3ui.widgets[listId].props.dataSource = {
                        items: res.data.data,
                        total: res.data.total
                    }
                });

            }
        })
    } else {
        let url = "/api/launcher/" + h3ui.appInfo.appCode + "/application/invoke?action=/ApplicationService/listBO";
        axiosService(url, param).then(res => {
            if (res.success) {
                h3ui.models[schemaCode].loadItemsRefProp(res.data.data);
                h3ui.widgets[listId].props.dataSource = {
                    items: res.data.data,
                    total: res.data.total
                }
            }
        })
    }
}


/**
 *  跳转表单页面并url携带参数
 *  pageCode // 页面code *
 *  param    // 参数
 *  appCode  // 应用code
 */
function toModePage(pageCode, param, appCode) {
    // 获取页面url地址
    let url = window.location.href.substring(0, window.location.href.indexOf('?'));

    if (appCode) {
        url = url.replaceAll(h3ui.appInfo.appCode, appCode);
    }

    var paramstr = Object.keys(param).map(a => {
        return "&".concat(a).concat("=").concat(param[a])
    }).join("");

    // 跳转创建页面
    window.location.href = url + '?pageCode=' + pageCode + paramstr;
}


//触发列表刷新事件
function reloadList(fromListId) {
    doReloadList({pageIndex: 1, node: {id: fromListId}});
}

//列表刷新
function doReloadList(evt) {
    var list = h3ui.widgets[evt.node.id];


    list.emit("load", queryListParam(list));


    list.props.selectedRowKeys = [];
    list.props.selectedRow = [];

}


function queryListParam(list) {
    var queryId = list.childIds.find(a => a.includes("ModelQueryForm"));

    var filter = h3ui.widgets[queryId].props.value || {};

    filter = JSON.parse(JSON.stringify(filter));

    var pageId = list.childIds.find(a => a.includes("Pagination"));

    var pageIndex = h3ui.widgets[pageId].props.current;
    var pageSize = h3ui.widgets[pageId].props.pageSize;

    return {pageIndex, pageSize, filter};
}


//自定义虚拟供应商报价商品加载事件
function custInventedOrderList(evt) {
    customConditionLoad(evt, [], "inventedVendorOrderList");
}


// 单元格文本渲染
function cellRender(evt) {
    // 获取模型表格id
    let modelTableId = evt.node.parentId;
    // 获取表单id
    let formListId = h3ui.widgets[modelTableId].parentId;
    let {components, col, row, formatText} = evt.data;
    let value = row[col.key];
    let nodeId = evt.data.col.node.id;
    if (["ModelTableColumn85", 'ModelTableColumn12', 'ModelTableColumn61'].includes(nodeId)) {

        var str = (row.package_quantity || 0) + '/' + (row.package_unit || "");
        components[0].props.content = str;

    } else if (vendorOrderStyle.includes(nodeId)) {

        if (formatText instanceof Array) {
            formatText = formatText[0].id;
        }

        setVendorOrderStyle(components, formatText);
    } else if (GoodsSrcStyle.includes(nodeId)) {
        debugger;
        if (evt.data.row.client_order_goods_id) {
            formatText = "对接";
        } else {
            formatText = "非对接";
        }
        setGoodsSrcStyle(components, formatText);
    } else if (VendorGoodsStatusStyle.includes(nodeId)) {
        setVendorGoodsStatusStyle(components, formatText)
    }

}

// 供应商订单序号样式
function setVendorOrderStyle(components, formatText) {
    components[0].props.fontColor = '#1890FF';
    components[0].props.fontSize = '14px';
   /* components[0].props.fontStyle = {
        textDecoration: 'underline'
    }*/
    components[0].props.content = formatText;
}

// 数据来源样式
function setGoodsSrcStyle(components, formatText) {

    components[0].props.fontColor = '#ff0000';
    components[0].props.fontSize = '14px';
    components[0].props.content = formatText;
}

// 商品状态样式
function setVendorGoodsStatusStyle(components, formatText) {

    var color = '#009688';
    components[0] = {
        type: 'Tag',
        props: {
            text: formatText,
            color
        }
    };
}


//行点击事件
function rowClick(evt) {
    console.log(evt);

    var key = evt.data.col.key;
    var value = evt.data.row[key];

    if (['order_vendor_id'].includes(key)) {
        var pageCode = 'order_vendor_id' == key ? "scmdetail" : "customer_detail_new";
        if (value instanceof Array) {
            value = value[0].id;
        }
        var param = {clickcloumn: value};
        var appCode = "ordercenter";
        toModePage(pageCode, param, appCode);

    }
}


//确认接单
function receivingOrder() {

    var rows=h3ui.widgets.FormList4.props.selectedRow
    if(rows.length==0){
        h3ui.$message.info("请勾选数据");
        return;

    }
    var orderVendorIds=[];
    rows.forEach(a=>{
        a.order_vendor_id.forEach(b=>{
            orderVendorIds.push(b.id)
        })
    })


    h3ui.$confirm({
        title: '确认接单吗?',
        okText: '确定',
        okType: 'primary',
        cancelText: '取消',
        onOk() {
            var url = "/api/launcher/ordercenter/application/invoke?action=/VendorOrderService/confirmReceipt";
            var pram = {ids: orderVendorIds, orderVendorStatus: 'UpcomingConfirm'};
            axiosService(url, pram).then(res => {
                if (res.success) {
                    h3ui.$message.success('提交成功');
                    reloadList("FormList4");
                }
            })

        },
        onCancel() {
            console.log('Cancel');
        }
    })

}

